﻿using System.Web.UI.WebControls;

namespace DMS.Business.Extends
{
    /// <summary>
    /// 提供GridView的一些处理方法，如合并行等操作
    /// </summary>
    public class GridViewHelper
    {
        private static GridViewHelper instance = new GridViewHelper();

        public static GridViewHelper Instance
        {
            get { return instance; }
            set { instance = value; }
        }
        private GridViewHelper() { }

        ///   <summary>   
        ///   合并GridView列中相同的行   
        ///   </summary>   
        ///   <param   name="GridView1">GridView对象</param>   
        ///   <param   name="cellNum">需要合并的列</param>   
        public void GroupRows(GridView GridView1, int cellNum)
        {
            int i = 0, rowSpanNum = 1;
            int index = 0;
            while (i < GridView1.Rows.Count - 1)
            {
                GridViewRow gvr = GridView1.Rows[i];
                //gvr.Cells[cellNum].BorderStyle = BorderStyle.Solid;
                for (++i; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow gvrNext = GridView1.Rows[i];

                    if (string.IsNullOrEmpty(gvr.Cells[cellNum].Text))
                    {
                        if (((Label)gvr.Cells[cellNum].Controls[1]).Text == ((Label)gvrNext.Cells[cellNum].Controls[1]).Text)
                        //if (((Label)gvr.Cells[cellNum].FindControl("lbMerge")).Text == ((Label)gvrNext.Cells[cellNum].FindControl("lbMerge")).Text)
                        {
                            gvrNext.Cells[cellNum].Style["display"] = "none";
                            rowSpanNum++;
                        }
                        else
                        {
                            gvr.Cells[cellNum].RowSpan = rowSpanNum;
                            rowSpanNum = 1;
                            break;
                        }
                    }
                    else
                    {
                        if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                        {
                            gvrNext.Cells[cellNum].Style["display"] = "none";
                            rowSpanNum++;
                        }
                        else
                        {
                            gvr.Cells[cellNum].RowSpan = rowSpanNum;
                            rowSpanNum = 1;
                            break;
                        }
                    }
                    if (i == GridView1.Rows.Count - 1)
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                        gvr.Cells[cellNum].Style["background-color"] = index % 2 == 1 ? "#FFFFFF" : "#E4F1FF";
                        index++;
                    }
                }
            }
        }
        
        ///   <summary>   
        ///   根据条件列合并GridView列中相同的行   
        ///   </summary>   
        ///   <param   name="GridView1">GridView对象</param>   
        ///   <param   name="cellNum">需要合并的列</param>
        ///   <param   name="conditionCellNum">条件列(根据某条件列合并)</param>
        public void GroupRows(GridView GridView1, int cellNum, int conditionCellNum)
        {
            int i = 0, rowSpanNum = 1;
            while (i < GridView1.Rows.Count - 1)
            {
                GridViewRow gvr = GridView1.Rows[i];
                gvr.Cells[cellNum].BorderStyle = BorderStyle.Solid;
                for (++i; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow gvrNext = GridView1.Rows[i];

                    string conditionText = gvr.Cells[conditionCellNum].Text;
                    string conditionTextNext = gvrNext.Cells[conditionCellNum].Text;
                    if (string.IsNullOrEmpty(conditionText))
                        conditionText = ((Label) gvr.Cells[conditionCellNum].Controls[1]).Text;
                    if (string.IsNullOrEmpty(conditionTextNext))
                        conditionTextNext = ((Label)gvrNext.Cells[conditionCellNum].Controls[1]).Text;

                    if (gvr.Cells[cellNum].Text + conditionText == gvrNext.Cells[cellNum].Text + conditionTextNext)
                    {
                        gvrNext.Cells[cellNum].Visible = false;
                        rowSpanNum++;
                    }
                    else
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                        rowSpanNum = 1;
                        break;
                    }

                    if (i == GridView1.Rows.Count - 1)
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    }
                }
            }
        }




        #region 复杂表头
        /// <summary> 
        /// 重写表头 
        /// </summary> 
        /// <param name="targetHeader">目标表头</param> 
        /// <param name="newHeaderNames">新表头</param> 
        /// <remarks> 
        /// </remarks> 
        public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames) 
        { 
            TableCellCollection tcl = targetHeader.Cells;//获得表头元素的实例 
            tcl.Clear();//清除元素 
            int row = GetRowCount(newHeaderNames); 
            int col = GetColCount(newHeaderNames); 
            string[,] nameList = ConvertList(newHeaderNames,row,col); 
            int RowSpan = 0; 
            int ColSpan = 0; 

            for (int k = 0; k < row; k++) 
            { 
                string LastFName = ""; 
                for (int i = 0; i < col; i++) 
                { 
                    if (LastFName == nameList[i, k] && k!=row-1) 
                    { 
                        LastFName = nameList[i, k]; 
                        continue; 
                    } 
                    else 
                    { 
                        LastFName = nameList[i, k]; 
                    } 
                    int bFlag=IsVisible(nameList, k, i, LastFName); 
                    switch (bFlag) 
                    { 
                        case 0: 
                            break; 
                        case 1: 
                            RowSpan = GetSpanRowCount(nameList,row, k, i); 
                            ColSpan = GetSpanColCount(nameList,row,col, k, i); 
                            tcl.Add(new TableHeaderCell());//添加表头控件 
                            tcl[tcl.Count - 1].RowSpan = RowSpan; 
                            tcl[tcl.Count - 1].ColumnSpan = ColSpan; 
                            tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center; 
                            tcl[tcl.Count - 1].Text = LastFName; 
                            break; 
                        case -1: 
                            string[] EndColName = LastFName.Split(new char[] { ',' }); 
                            foreach(string eName in EndColName){ 
                                tcl.Add(new TableHeaderCell());//添加表头控件 
                                tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center; 
                                tcl[tcl.Count - 1].Text = eName; 
                            } 
                            break; 
                    } 
                } 
                if (k != row-1) 
                {//不是起始行,加入新行标签 
                    tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text+"</th></tr><tr class="+targetHeader.CssClass+">"; 
                } 
            } 
        } 
        /**//// <summary> 
        /// 如果上一行已经输出和当前内容相同的列头，则不显示 
        /// </summary> 
        /// <param name="ColumnList">表头集合</param> 
        /// <param name="rowIndex">行索引</param> 
        /// <param name="colIndex">列索引</param> 
        /// <returns>1:显示,-1:含','分隔符,0:不显示</returns> 
        private int IsVisible(string[,] ColumnList,int rowIndex, int colIndex,string CurrName) 
        { 
            if (rowIndex!=0){ 
                if (ColumnList[colIndex,rowIndex-1]==CurrName){ 
                    return 0; 
                }else{ 
                    if (ColumnList[colIndex, rowIndex].Contains(",")) 
                    { 
                        return -1; 
                    }else{ 
                        return 1; 
                    } 
                } 
            } 
            return 1; 
        } 
        /**//// <summary> 
        /// 取得和当前索引行及列对应的下级的内容所跨的行数 
        /// </summary> 
        /// <param name="ColumnList">表头集合</param> 
        /// <param name="row">行数</param> 
        /// <param name="rowIndex">行索引</param> 
        /// <param name="colIndex">列索引</param> 
        /// <returns>行数</returns> 
        private int GetSpanRowCount(string[,] ColumnList, int row,int rowIndex, int colIndex) 
        { 
            string LastName = ""; 
            int RowSpan = 1; 
            for (int k = rowIndex; k < row; k++) 
            { 
                if (ColumnList[colIndex,k]==LastName){ 
                    RowSpan++; 
                }else{ 
                    LastName = ColumnList[colIndex, k]; 
                } 
            } 
            return RowSpan; 
        } 
        /**//// <summary> 
        /// 取得和当前索引行及列对应的下级的内容所跨的列数 
        /// </summary> 
        /// <param name="ColumnList">表头集合</param> 
        /// <param name="row">行数</param> 
        /// <param name="col">列数</param> 
        /// <param name="rowIndex">行索引</param> 
        /// <param name="colIndex">列索引</param> 
        /// <returns>列数</returns> 
        private int GetSpanColCount(string[,] ColumnList,int row, int col,int rowIndex, int colIndex) 
        { 
            string LastName = ColumnList[colIndex, rowIndex] ; 
            int ColSpan = ColumnList[colIndex, row-1].Split(new char[] { ',' }).Length; 
            ColSpan = ColSpan == 1 ? 0 : ColSpan; 
            for(int i=colIndex+1;i<col;i++) 
            { 
                if (ColumnList[i, rowIndex] == LastName) 
                { 
                    ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length; 
                } 
                else 
                { 
                    LastName = ColumnList[i, rowIndex]; 
                    break; 
                } 
            } 
            return ColSpan; 
        } 
        /**//// <summary> 
        /// 将已定义的表头保存到数组 
        /// </summary> 
        /// <param name="newHeaders">新表头</param> 
        /// <param name="row">行数</param> 
        /// <param name="col">列数</param> 
        /// <returns>表头数组</returns> 
        private string[,] ConvertList(string newHeaders, int row, int col) 
        { 
            string[] ColumnNames = newHeaders.Split(new char[] {'#'}); 
            string[,] news = new string[col, row]; 
            string Name = ""; 
            for (int i = 0; i < col; i++) 
            { 
                string[] CurrColNames = ColumnNames[i].ToString().Split(new char[] { '|' }); 
                for (int k = 0; k < row; k++) 
                { 
                    if (CurrColNames.Length - 1 >= k) 
                    { 
                        if (CurrColNames[k].Contains(",")) 
                        { 
                            if (CurrColNames.Length != row) 
                            { 
                                if (Name == "") 
                                { 
                                    news[i, k] = news[i, k - 1]; 
                                    Name = CurrColNames[k].ToString(); 
                                } 
                                else 
                                { 
                                    news[i, k + 1] = Name; 
                                    Name = ""; 
                                } 
                            }else{ 
                                news[i, k] = CurrColNames[k].ToString(); 
                            } 
                        }else{ 
                            news[i, k] = CurrColNames[k].ToString(); 
                        } 
                    }else{ 
                        if (Name == "") 
                        { 
                            news[i, k] = news[i, k - 1]; 
                        }else{ 
                            news[i, k] = Name; 
                            Name = ""; 
                        } 
                    } 
                } 
            } 
            return news; 
        } 
        /**//// <summary> 
        /// 取得复合表头的行数 
        /// </summary> 
        /// <param name="newHeaders">新表头</param> 
        /// <returns>行数</returns> 
        private int GetRowCount(string newHeaders) 
        { 
            string[] ColumnNames = newHeaders.Split(new char[] { '#' }); 
            int Count = 0; 
            foreach(string name in ColumnNames){ 
                int TempCount = name.Split(new char[] { '|' }).Length; 
                if (TempCount > Count) 
                    Count = TempCount; 
            } 
            return Count; 
        } 
        /**//// <summary> 
        /// 取得复合表头的列数 
        /// </summary> 
        /// <param name="newHeaders">新表头</param> 
        /// <returns>列数</returns> 
        private int GetColCount(string newHeaders) 
        { 
            return newHeaders.Split(new char[] { '#' }).Length;
        }
        #endregion
    }
}
